package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.huashigang.HuaShiGangActivityEventConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Author wpj
 * @Date 2020/10/6
 */
@Cmd(code = GameProtocol.S_HUA_SHI_GANG_TRADE_FROM_CROSS, desc = "从跨服收到花石纲贸易")
public class TradeFromCrossCmd extends AbstractCommand {
    int eventId;
    int activityId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.TradeExchangeFromCross msg = HuaShiGangProto.TradeExchangeFromCross.parseFrom(packet.getBytes());
        eventId = msg.getEventId();
        activityId = msg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        HuaShiGangActivityEventConfig config = HuaShiGangMgr.getHuaShiGangActivityEventConfigMap().get(eventId);
        CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
        if (currencyModule.removeCurrency(PropertyHelper.parseStringToProperty(config.getParamList()), eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangTrade)) {
            currencyModule.addCurrency(PropertyHelper.parseStringToProperty(config.getRewards()), eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangTrade);
        } else {
            log.info("item not enough {} {} {}", activityId, packet.getPlayerId(), eventId);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        HuaShiGangProto.TradeExchangeRespMsg.Builder builder = HuaShiGangProto.TradeExchangeRespMsg.newBuilder();
        builder.setRet(0);
        player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_TRADE, builder);

    }
}
